// sm3_hash.c
#include <stdio.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include "utils.h"

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
        exit(1);
    }

    size_t len;
    unsigned char *data = read_file(argv[1], &len);

    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    const EVP_MD *md = EVP_sm3();
    unsigned char dgst[32];
    unsigned int dgst_len;

    if (!EVP_DigestInit_ex(ctx, md, NULL) ||
        !EVP_DigestUpdate(ctx, data, len) ||
        !EVP_DigestFinal_ex(ctx, dgst, &dgst_len)) {
        fprintf(stderr, "SM3 hash failed.\n");
        exit(1);
    }

    for (int i = 0; i < dgst_len; i++) {
        printf("%02x", dgst[i]);
    }
    printf("\n");

    EVP_MD_CTX_free(ctx);
    free(data);
    return 0;
}
